/*
 ============================================================================
 Name		: FM_RadioAppUi.cpp
 Author	  : kamal
 Copyright   : Your copyright notice
 Description : CFM_RadioAppUi implementation
 ============================================================================
 */
// INCLUDE FILES
#include <avkon.hrh>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <stringloader.h>
#include <f32file.h>
#include <s32file.h>
#include <hlplch.h>
#include <FM_Radio_0xE9D11917.rsg>
#ifdef _HELP_AVAILABLE_
#include "FM_Radio_0xE9D11917.hlp.hrh"
#endif
#include "FM_Radio.hrh"
#include "FM_Radio.pan"
#include "FM_RadioApplication.h"
#include "FM_RadioAppUi.h"
#include "FM_RadioAppView.h"
_LIT( KFileName, "C:\\private\\E9D11917\\FM_Radio.txt" );
_LIT( KText, "Hello World!");

// ============================ MEMBER FUNCTIONS ===============================


// -----------------------------------------------------------------------------
// CFM_RadioAppUi::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFM_RadioAppUi::ConstructL()
{
	// Initialise app UI with standard value.
	BaseConstructL(CAknAppUi::EAknEnableSkin);

	// Create view object
	iAppView = CFM_RadioAppView::NewL(ClientRect());

	InitializeRadio();

	// Create a file to write the text to
	TInt err = CCoeEnv::Static()->FsSession().MkDirAll(KFileName);
	if ((KErrNone != err) && (KErrAlreadyExists != err))
	{
		return;
	}

	RFile file;
	err = file.Replace(CCoeEnv::Static()->FsSession(), KFileName, EFileWrite);
	CleanupClosePushL(file);
	if (KErrNone != err)
	{
		CleanupStack::PopAndDestroy(1); // file
		return;
	}

	RFileWriteStream outputFileStream(file);
	CleanupClosePushL(outputFileStream);
	outputFileStream << KText;

	CleanupStack::PopAndDestroy(2); // outputFileStream, file

}
// -----------------------------------------------------------------------------
// CFM_RadioAppUi::CFM_RadioAppUi()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CFM_RadioAppUi::CFM_RadioAppUi()
{
	// No implementation required
}

// -----------------------------------------------------------------------------
// CFM_RadioAppUi::~CFM_RadioAppUi()
// Destructor.
// -----------------------------------------------------------------------------
//
CFM_RadioAppUi::~CFM_RadioAppUi()
{

	if (iFmTunerUtility)
	{
		iFmTunerUtility->Close();
	}

	if (iPlayerUtility)
	{
		iPlayerUtility->Close();
	}

	delete iRadioUtility;
	iRadioUtility = NULL;

	if (iAppView)
	{
		delete iAppView;
		iAppView = NULL;
	}

}

// -----------------------------------------------------------------------------
// CFM_RadioAppUi::HandleCommandL()
// Takes care of command handling.
// -----------------------------------------------------------------------------
//
void CFM_RadioAppUi::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
		case EEikCmdExit:
		case EAknSoftkeyExit:
			Exit();
			break;

		case ECommand1:
		{

			// Load a string from the resource file and display it
			/*HBufC* textResource = StringLoader::LoadLC(R_COMMAND1_TEXT);
			 CAknInformationNote* informationNote;

			 informationNote = new (ELeave) CAknInformationNote;

			 // Show the information Note with
			 // textResource loaded with StringLoader.
			 informationNote->ExecuteLD(*textResource);

			 // Pop HBuf from CleanUpStack and Destroy it.
			 CleanupStack::PopAndDestroy(textResource);
			 */

			StartRadio();
		}
			break;
		case ECommand2:
		{
			/*RFile rFile;

			 //Open file where the stream text is
			 User::LeaveIfError(rFile.Open(CCoeEnv::Static()->FsSession(), KFileName, EFileStreamText));//EFileShareReadersOnly));// EFileStreamText));
			 CleanupClosePushL(rFile);

			 // copy stream from file to RFileStream object
			 RFileReadStream inputFileStream(rFile);
			 CleanupClosePushL(inputFileStream);

			 // HBufC descriptor is created from the RFileStream object.
			 HBufC* fileData = HBufC::NewLC(inputFileStream, 32);

			 CAknInformationNote* informationNote;

			 informationNote = new (ELeave) CAknInformationNote;
			 // Show the information Note
			 informationNote->ExecuteLD(*fileData);

			 // Pop loaded resources from the cleanup stack
			 CleanupStack::PopAndDestroy(3); // filedata, inputFileStream, rFile*/

			StopRadio();
		}
			break;
		case EHelp:
		{

			/*CArrayFix<TCoeHelpContext>* buf = CCoeAppUi::AppHelpContextL();
			 HlpLauncher::LaunchHelpApplicationL(iEikonEnv->WsSession(), buf);*/

			TInt CurrentFrequency;
			if (iFmTunerUtility)
				iFmTunerUtility->GetFrequency(CurrentFrequency);

			CurrentFrequency++;

			if (iFmTunerUtility)
				iFmTunerUtility->SetFrequency(CurrentFrequency);
			TBuf<100> Buffer;
			Buffer.Format(_L("%d"), CurrentFrequency);
			iEikonEnv->InfoMsg(Buffer);

		}
			break;
		case EAbout:
		{

			CAknMessageQueryDialog* dlg = new (ELeave) CAknMessageQueryDialog();
			dlg->PrepareLC(R_ABOUT_QUERY_DIALOG);
			HBufC* title = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TITLE);
			dlg->QueryHeading()->SetTextL(*title);
			CleanupStack::PopAndDestroy(); //title
			HBufC* msg = iEikonEnv->AllocReadResourceLC(R_ABOUT_DIALOG_TEXT);
			dlg->SetMessageTextL(*msg);
			CleanupStack::PopAndDestroy(); //msg
			dlg->RunLD();
		}
			break;
		default:
			Panic(EFM_RadioUi);
			break;
	}
}
// -----------------------------------------------------------------------------
//  Called by the framework when the application status pane
//  size is changed.  Passes the new client rectangle to the
//  AppView
// -----------------------------------------------------------------------------
//
void CFM_RadioAppUi::HandleStatusPaneSizeChange()
{
	iAppView->SetRect(ClientRect());
}

CArrayFix<TCoeHelpContext>* CFM_RadioAppUi::HelpContextL() const
{
#warning "Please see comment about help and UID3..."
	// Note: Help will not work if the application uid3 is not in the
	// protected range.  The default uid3 range for projects created
	// from this template (0xE0000000 - 0xEFFFFFFF) are not in the protected range so that they
	// can be self signed and installed on the device during testing.
	// Once you get your official uid3 from Symbian Ltd. and find/replace
	// all occurrences of uid3 in your project, the context help will
	// work. Alternatively, a patch now exists for the versions of 
	// HTML help compiler in SDKs and can be found here along with an FAQ:
	// http://www3.symbian.com/faq.nsf/AllByDate/E9DF3257FD565A658025733900805EA2?OpenDocument
#ifdef _HELP_AVAILABLE_
	CArrayFixFlat<TCoeHelpContext>* array = new (ELeave) CArrayFixFlat<TCoeHelpContext> (1);
	CleanupStack::PushL(array);
	array->AppendL(TCoeHelpContext(KUidFM_RadioApp, KGeneral_Information));
	CleanupStack::Pop(array);
	return array;
#else
	return NULL;
#endif
}

TBool CFM_RadioAppUi::InitializeRadio()
{
	TRAPD(utilityError,iRadioUtility = CRadioUtility::NewL(ETrue);
		// Get a tuner utility
		iFmTunerUtility = &iRadioUtility->RadioFmTunerUtilityL(*this);
		// Get a player utility
		iPlayerUtility = &iRadioUtility->RadioPlayerUtilityL(*this);
	);

	iFmTunerUtility->EnableTunerInOfflineMode(ETrue);
	//iFmTunerUtility->SetFrequency(94);
	//iFmTunerUtility->SetFrequencyRange(EFmRangeEuroAmerica);
	iFmTunerUtility->RequestTunerControl();
	iFmTunerUtility->StationSeek(ETrue);

}

//MRadioPlayerObserver
void CFM_RadioAppUi::MrpoStateChange(TPlayerState aState, TInt aError)
{
	if (aState == ERadioPlayerIdle)
		aState = ERadioPlayerPlaying;

}

//MRadioPlayerObserver
void CFM_RadioAppUi::MrpoVolumeChange(TInt aVolume)
{

}
//MRadioPlayerObserver
void CFM_RadioAppUi::MrpoMuteChange(TBool aMute)
{

}

//MRadioPlayerObserver
void CFM_RadioAppUi::MrpoBalanceChange(TInt aLeftPercentage, TInt aRightPercentage)
{

}

//MRadioFmTunerObserver

void CFM_RadioAppUi::MrftoRequestTunerControlComplete(TInt aError)
{

}
void CFM_RadioAppUi::MrftoSetFrequencyRangeComplete(TInt aError)
{

}
void CFM_RadioAppUi::MrftoSetFrequencyComplete(TInt aError)
{

}
void CFM_RadioAppUi::MrftoStationSeekComplete(TInt aError, TInt aFrequency)
{

}
void CFM_RadioAppUi::MrftoFmTransmitterStatusChange(TBool aActive)
{

}
void CFM_RadioAppUi::MrftoAntennaStatusChange(TBool aAttached)
{

}
void CFM_RadioAppUi::MrftoOfflineModeStatusChange(TBool aOfflineMode)
{

}
void CFM_RadioAppUi::MrftoFrequencyRangeChange(TFmRadioFrequencyRange aBand /*, TInt aMinFreq, TInt aMaxFreq*/)
{

}
void CFM_RadioAppUi::MrftoFrequencyChange(TInt aNewFrequency)
{

}
void CFM_RadioAppUi::MrftoForcedMonoChange(TBool aForcedMono)
{

}
void CFM_RadioAppUi::MrftoSquelchChange(TBool aSquelch)
{

}

void CFM_RadioAppUi::StartRadio()
{
	if (iPlayerUtility)
		iPlayerUtility->Play();

}
void CFM_RadioAppUi::StopRadio()
{
	if (iPlayerUtility)
		iPlayerUtility->Stop();

}

// End of File
